﻿function view_onOpen(first) {
	setDefault();
	getSaved();
	redraw(first);
	nome.innerText=options.getValue("victim");
	vicpic.src=options.getValue("vicpic");
	switch (options.getValue("showsign")){
		case "showname":
			vicpic.visible=false;
			alfplaca.visible=true;
			nome.visible=true;
			placa.visible=true;
			break;
		case "showpic":
			vicpic.visible=true;
			alfplaca.visible=true;
			nome.visible=false;
			placa.visible=false;
			break;
		case "shownone":
			vicpic.visible=false;
			alfplaca.visible=false;
			nome.visible=false;
			placa.visible=false;
			break;
	}
}
function getSaved(){
	//get saved pins
	alfinete=options.getValue("alfinete");
	if (alfinete.length>0){
		if (typeof(alfinete)=="string"){
			alfinete=alfinete.split(",");
		}
	}else{
		alfinete=new Array();
	}
}
function redraw(first){
	if (!first){
		//destroy old pins
		var e = new Enumerator(pins.children);
    while (!e.atEnd()) {
      pins.removeElement(e.item());
      e.moveNext();
    }
	}
	//Total
	//view.caption=options.getValue("victim")+" - "+alfinete.length+" "+PINS;
	//Create pins
	for (var i=0;i<alfinete.length;i++){
		cur=alfinete[i].split("|");
		for (var j=0;j<cur.length;j++){
			cur[0]=parseInt(cur[0]);
			cur[1]=parseInt(cur[1]);
			if (cur[0]>=180){
				rotation=" rotation='60'";
			}else if (cur[0]>=135 && cur[0]<180){
				rotation=" rotation='30'";
			}else if (cur[0]>=80 && cur[0]<100){
				rotation=" rotation='100'";
			}else{
				rotation="";
			}
			pins.appendElement("<img enabled='true' name='"+cur[0]+"|"+cur[1]+"' x='"+cur[0]+"' y='"+cur[1]+"' src='stock_images/alfinete.png' width='46' height='36' onclick='removeAlf(\""+cur[0]+"|"+cur[1]+"\")'"+rotation+" />");
		}
	}
}
function putAlf(){
	getSaved();
	//Add and count max elements of array
	alfinete.push(event.x+"|"+event.y);
	//Save values
	alfinete=alfinete.join(",");
	options.putValue("alfinete",alfinete);
	//redraw();
}
function removeAlf(alfID){
	getSaved();
	var remove=alfinete.indexOf(alfID);
	if (remove!=-1){
		alfinete.splice(remove,1);
		//Save values
		alfinete=alfinete.join(",");
		options.putValue("alfinete",alfinete);
		//redraw();
	}
}
// Credits for indexOf: http://www.4umi.com/web/javascript/array.htm#indexof
// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};
